package com.zhinong.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhinong.dto.ProductPageQueryDTO;
import com.zhinong.entity.Employee;
import com.zhinong.entity.Product;
import com.zhinong.mapper.ProductMapper;
import com.zhinong.result.PageResult;
import com.zhinong.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public void save(Product product) {
        productMapper.insert(product);
    }

    @Override
    public PageResult pageQuery(ProductPageQueryDTO productPageQueryDTO) {
        if(productPageQueryDTO.getPage() == 0 && productPageQueryDTO.getPageSize() == 0){
            productPageQueryDTO.setPage(1);
            productPageQueryDTO.setPageSize(20);
        }
        PageHelper.startPage(productPageQueryDTO.getPage(),productPageQueryDTO.getPageSize());
        Page<Product> page = productMapper.pageQuery(productPageQueryDTO);
        long total = page.getTotal();
        List<Product> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void deleteById(Long productId) {
        productMapper.deteleById(productId);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);

    }

    @Override
    public Product getByID(Long ids) {
        return null;
    }

    @Override
    public List<Product> getByIds(List<Long> ids) {
        List<Product> products = productMapper.getByIds(ids);
        return products;
    }


}
